package project.pianpian.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import project.pianpian.entity.*;
import project.pianpian.mapper.*;
import project.pianpian.utils.ReturnUtils;

@RestController
@Slf4j
@RequestMapping("/main")
public class UserWatching {
    @Autowired
    VideoMapper videoMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    UpvoteMapper upvoteMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    FollowMapper followMapper;

    //获取个人详情页面
    @GetMapping("/user")
    public JSONObject watchVideo(@RequestParam(value = "userId") int userId){

        //获取个人信息
        User user = userMapper.selectById(userId);

        //获取点赞的视频数量
        QueryWrapper<Upvote> upvoteQueryWrapper=new QueryWrapper<>();
        upvoteQueryWrapper.eq("upvoter_id", user.getId());
        int upvoteCount = upvoteMapper.selectCount(upvoteQueryWrapper);

        //获取收藏的视频数量
        QueryWrapper<Collect> collectQueryWrapper=new QueryWrapper<>();
        collectQueryWrapper.eq("collector_id", user.getId());
        int collectCount= collectMapper.selectCount(collectQueryWrapper);

        //获取个人粉丝数
        QueryWrapper<Follow> followQueryWrapper=new QueryWrapper<>();
        followQueryWrapper.eq("follower_id", user.getId());
        int followCount=followMapper.selectCount(followQueryWrapper);

        //将以上查询结果封装入JSON方便返回
        JSONObject returnJSON=new JSONObject();
        returnJSON.put("User", user);
        //returnJSON.put("upvoteVideo", upvoteVideo);
        //returnJSON.put("collectVideo", collectVideo);
        returnJSON.put("upvoteCount",upvoteCount);
        returnJSON.put("collectCount",collectCount);
        returnJSON.put("followCount",followCount);

        return ReturnUtils.success(200,"获取个人页面详情成功",returnJSON);
    }
}
